﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Common
{
    //cross cutting
    public class CheckDublicateQuestion
    {
        public bool CheckDublicate(List<Question> questionList, Question question)
        {
            var isEqual = true;
            List<Question> listQuestion = new List<Question>();
            List<Answer> answers, questionListAnswers;
            List<TestOnlineProject.Models.Entities.Match> matches, questionListMatches;
            var isEqualAnswer = true;
            var isEqualMatch = true;
            if (questionList.Count == 0)
            {
                isEqual = false;
            }
            for (int i = 0; i < questionList.Count; i++)
            {
                isEqual = true;
                //check question content
                if (question.Content != questionList[i].Content)
                {
                    isEqual = false;
                }
                else
                {
                    //Check match
                    matches = question.Matches.ToList();
                    questionListMatches = questionList[i].Matches.ToList();
                    //check number of matches
                    if (matches.Count == questionListMatches.Count)
                    {
                        //check match content
                        for (int k = 0; k < questionListMatches.Count; k++)
                        {
                            isEqualMatch = false;
                            for (int l = 0; l < matches.Count; l++)
                            {

                                if (matches[l].Content == questionListMatches[k].Content)
                                {
                                    isEqualMatch = true;
                                    break;
                                }

                            }
                            if (isEqualMatch == false)
                            {
                                isEqual = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        isEqual = false;
                    }
                    if (isEqualMatch == true)
                    {
                        //check answer
                        answers = question.Answers.ToList();
                        questionListAnswers = questionList[i].Answers.ToList();
                        //check number of answers
                        if (answers.Count == questionListAnswers.Count)
                        {
                            //check answer content
                            for (int k = 0; k < questionListAnswers.Count; k++)
                            {
                                isEqualAnswer = false;
                                for (int l = 0; l < answers.Count; l++)
                                {
                                    if (answers[l].Content == questionListAnswers[k].Content)
                                    {
                                        if (answers[l].Grade == questionListAnswers[k].Grade)
                                        {
                                            //check match content of answer
                                            if (answers[l].Match != null)
                                            {
                                                if (questionListAnswers[k].Match != null)
                                                {
                                                    if (answers[l].Match.Content == questionListAnswers[k].Match.Content)
                                                    {
                                                        isEqualAnswer = true;
                                                        break;
                                                    }
                                                }
                                                else
                                                {
                                                    isEqualAnswer = false;
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                if (questionListAnswers[k].Match == null)
                                                {
                                                    isEqualAnswer = true;
                                                    break;
                                                }
                                                else
                                                {
                                                    isEqualAnswer = false;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isEqualAnswer == false)
                                {
                                    isEqual = false;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            isEqual = false;
                        }
                    }

                }
                if (isEqual == true)
                {
                    return isEqual;
                }
            }
            return isEqual;
        }
    }
}